<?php
namespace App\Filters;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
/**
 * Copyright (C), 2021-2021, old_liu_cms.
 * FileName: QueryFilter.php
 * Description: 说明
 *
 * @author old_liu_cms
 * @Create Date    2021/10/11 09:34
 * @Update Date    2021/10/11 09:34 By old_liu_cms
 * @version v1.0
 */
abstract class QueryFilter
{
    protected $request;
    protected $builder;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    public function apply(Builder $builder)
    {
        $this->builder = $builder;

        foreach ($this->filters() as $name => $value) {
            if (method_exists($this, $name)) {
                call_user_func_array([$this, $name], array_filter([$value]));
            }
        }

        return $this->builder;
    }

    public function filters()
    {
        return $this->request->all();
    }
}
